package com.cxl.dao;

import com.cxl.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 20971
 * @time: 2022/3/1 21:36
 * @description:
 */

@Repository
public class UserDao {


    @Autowired
    private EntityManager entityManager;


    //新增,修改
    @Transactional
    public void save(User user) {
        //将要批量插入的实例转换为managed(托管)状态
        entityManager.merge(user);
        /** 提交事务，实例将会被插入到数据库中 */
        entityManager.flush();
        entityManager.clear();
    }

    //添加
    @Transactional
    public void add(User user) {
        //将要批量插入的实例转换为managed(托管)状态
        entityManager.persist(user);
        /** 提交事务，实例将会被插入到数据库中 */
        entityManager.flush();
        entityManager.clear();
    }

    //修改
    @Transactional
    public void update(User user) {
        //将要批量更新的实例转换为managed(托管)状态
        entityManager.merge(user);
        /** 提交事务，实例将会被插入到数据库中 */
        entityManager.flush();
        entityManager.clear();
    }

    //删除
    @Transactional
    public void del(User user){
        //移除已经持久化的对象
        entityManager.remove(entityManager.merge(user));
        /** 提交事务，实例将会被插入到数据库中 */
        entityManager.flush();
        entityManager.clear();
    }

    //查询所有
    @Transactional
    public List<User> findAll(){
//        String str = "";
        Query from_user_u = entityManager.createQuery("from User u");
        /** 提交事务，实例将会被插入到数据库中 */
        entityManager.flush();
        entityManager.clear();
        return from_user_u.getResultList();
    }


    //模糊查询
    @Transactional
    public List<User> queryUser() {

        Query query = entityManager.createQuery("from User u where name like concat('%','崔','%')");
        return query.getResultList();


        /*Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "小崔");

        User user = entityManager.find(User.class, map);
*/

//        return null;
/*        String sql = "select * from t_user where name like concat ('%','崔','%')";
        Query namedQuery = entityManager.createNamedQuery(sql);
        return namedQuery.getResultList();*/


       /* CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<String> query = criteriaBuilder.createQuery(String.class);
        Root<User> from = query.from(User.class);

        //查询单个参数
        query.select(from.get("name")).where(criteriaBuilder.equal(from.get("name"), "崔"));

        //查询结果
        String singleResult = entityManager.createQuery(query).getSingleResult();

//        String sql = "select t.name name from t_user t where t.name like '%'||:name||'%'";
        System.out.println(singleResult);
        return null;
      *//*  //执行查询 并把结果专为实体类
        TypedQuery<User> namedQuery = entityManager.createNamedQuery(sql, User.class);
        namedQuery.setParameter("name", 1);
        //获取查询结果集
        return namedQuery.getResultList();*/
    }
}
